package com.customview.wzq.recycleviewdemo;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.support.v7.widget.RecyclerView;
import android.view.View;

/**
 * 创建时间：2017/7/16
 * 作者：kb_jay
 * 功能描述：实现grid 分割线
 * tIP1：需要知道列数，
 * 最后一列的右边要求没有分割线，
 * 最后一行的下面要求没有分割线
 */

public class GridDivider extends RecyclerView.ItemDecoration {

    private final Drawable mDivider;
    private Context mContext;
    private int mNumber;
    private final int space;

    public GridDivider(Context context, int number) {
        this.mContext = context;
        this.mNumber = number;
        mDivider = context.getResources().getDrawable(R.drawable.divider);
        space = mDivider.getIntrinsicHeight();
    }


    @Override
    public void getItemOffsets(Rect outRect, int itemPosition, RecyclerView parent) {
        super.getItemOffsets(outRect, itemPosition, parent);
        int left = 0;
        int top = 0;
        int right = mDivider.getIntrinsicWidth();
        int bottom = mDivider.getIntrinsicHeight();
       /* if (isLastCol(itemPosition)) {
            right = 0;
        }
        if (isLastRaw(itemPosition, parent.getChildCount())) {
            bottom = 0;
        }*/
        outRect.set(left, top, right, bottom);
    }

    private boolean isLastRaw(int position, int total) {
        if (total - 1 - position <= mNumber)
            return true;
        return false;
    }


    @Override
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
        super.getItemOffsets(outRect, view, parent, state);
    }

    private boolean isLastCol(int itemPosition) {
        if ((itemPosition + 1) % mNumber == 0) {
            return true;
        }
        return false;
    }

    @Override
    public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
        drawHor(c, parent, state);
        drawVer(c, parent, state);
    }

    private void drawVer(Canvas c, RecyclerView parent, RecyclerView.State state) {
        for (int i = 0; i < parent.getChildCount(); i++) {
            View child = parent.getChildAt(i);
            RecyclerView.LayoutParams lp = (RecyclerView.LayoutParams) child.getLayoutParams();
            int left = child.getRight() + lp.rightMargin;
            int right = left + space;
            int top = child.getTop() - lp.topMargin;
            int bottom = child.getBottom() + lp.bottomMargin;
            mDivider.setBounds(left, top, right, bottom);
            mDivider.draw(c);
        }
    }

    private void drawHor(Canvas c, RecyclerView parent, RecyclerView.State state) {
        for (int i = 0; i < parent.getChildCount(); i++) {
            View view = parent.getChildAt(i);
            RecyclerView.LayoutParams lp = (RecyclerView.LayoutParams) view.getLayoutParams();
            int left = view.getLeft() - lp.leftMargin;
            int right = view.getRight() + lp.rightMargin + mDivider.getIntrinsicWidth();
            int top = view.getBottom() + lp.bottomMargin;
            int bottom = top + space;
            mDivider.setBounds(left, top, right, bottom);
            mDivider.draw(c);
        }
    }

    public boolean isNeedVerDivider(int position) {
        if ((position + 1) % mNumber == 0)
            return false;
        return true;
    }

    public boolean isNeedHorDivider(int position, int total) {
        if (total - 1 - position <= mNumber)
            return false;
        return true;
    }

}
